package com.example.demo.jvm.class3;

/**
 * @author liweizhi
 * @date 2020/5/4
 */
public class CasheLineSample001 {
    static final long TIMES = 1000_0000L;

    static void originalMethod() throws InterruptedException {
        long[] arr = new long[2];
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < TIMES; i++) {
                arr[0] = i;
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < TIMES; i++) {
                arr[1] = i;
            }
        });
        long start = System.currentTimeMillis();
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long end = System.currentTimeMillis();
        System.out.println("originalMethod :" + (end - start));
    }

    static class Param {
        protected long l1, l2, l3, l4, l5, l6, l7;
        long num;
    }

    static void optimizedMethod() throws InterruptedException {
        Param[] arr = {new Param(), new Param()};
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < TIMES; i++) {
                arr[0].num = i;
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < TIMES; i++) {
                arr[1].num = i;
            }
        });
        long start = System.currentTimeMillis();
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long end = System.currentTimeMillis();
        System.out.println("optimizedMethod :" + (end - start));
    }

    public static void main(String[] args) throws InterruptedException {
        originalMethod();
        optimizedMethod();
    }
}
